var Test = require('../handlers/testHandler.js')
var config = require('../config/config.js')
var util = require('../utility/util')

module.exports = {

  //  获取所有用户练习
  //  三种获取方法，获取全部， 根据用户名获取 user ，根据练习等级获取 leavel
  getTestList: function(req, res) {
    var filter = null
    var query = req.query
    var params = req.params
    var type = params.type,
      page = params.page

    if (query) {
      filter = query
    }

    if (!util.isValidTestType(type)) {
      return res.json({
        code: 122,
        msg: '非法类型'
      })
    }
    Test.findAll(type, page, filter, function(err, testList) {
      if (err) {
        console.log('testController getTestList fail. err = ' + err)
        return res.json({
          code: 177,
          msg: err
        })
      }
      if (!testList.length) {
        return res.json({
          code: 112,
          msg: '没有数据'
        })
      }
      return res.json({
        code: 100,
        data: {
          list: testList
        }
      })
    })
  },
  //  创建新习题
  create: function(req, res) {
    var body = req.body
    var type = req.params.type

    if (!util.isValidTestType(type)) {
      return res.json({
        code: 122,
        msg: '非法类型'
      })
    }

    Test.insert(type, body.content, body.userId, function(err, newTest) {
      if (err) {
        console.log('testController create fail. err = ' + err)
        return res.json({
          code: 177,
          msg: err
        })
      }
      return res.json({
        code: 100,
        msg: '添加成功'
      })
    })
  },
  getOneByName: function(req, res) {
    var testName = req.params.testName
    var type = req.params.type
    var userId = req.query.userId

    if (!testName || !type) {
      return res.json({
        code: 122,
        msg: '参数错误'
      })
    }

    if (!util.isValidTestType(type)) {
      return res.json({
        code: 122,
        msg: '非法类型'
      })
    }

    Test.findByName({
      testName: testName,
      type: type,
      userId: userId
    }, function(err, test) {
      if (err) {
        console.log('testController getOne fail. err = ' + err)
        return res.json({
          code: 177,
          msg: err
        })
      }
      if (!test) {
        return res.json({
          code: 112,
          msg: '没有习题数据'
        })
      }
      return res.json({
        code: 100,
        data: {
          test: test
        }
      })

    })

  },
  getOneById: function(req, res) {
    var id = req.params.id
    var type = req.params.type

    if (!id || !type) {
      return res.json({
        code: 122,
        msg: '参数错误'
      })
    }

    if (!util.isValidTestType(type)) {
      return res.json({
        code: 122,
        msg: '非法类型'
      })
    }

    Test.findById({
      id: id,
      type: type,
      userId: req.query.userId
    }, function(err, test) {
      if (err) {
        console.log('testController getOne fail. err = ' + err)
        return res.json({
          code: 177,
          msg: err
        })
      }
      if (!test) {
        return res.json({
          code: 112,
          msg: '没有习题数据'
        })
      }
      return res.json({
        code: 100,
        data: {
          test: test
        }
      })

    })

  },
  /**
   * 后台更新习题和答案接口
   */
  update: function(req, res) {
    var body = req.body
    var type = req.params.type

    if (!util.isValidTestType(type)) {
      return res.json({
        code: 122,
        msg: '非法类型'
      })
    }

    Test.findByIdAndUpdate({
      type: type
    }, body, function(err, newTest) {
      if (err) {
        console.log('testController update fail. err = ' + err)
        return res.json({
          code: 177,
          msg: err
        })
      } else {
        return res.json({
          code: 100,
          data: {
            test: newTest
          }
        })
      }
    })
  },
  /**
   * 三种习题数据删除接口
   */
  remove: function(req, res) {
    var params = req.params
    var type = params.type
    var id = params.id
      //  删除用户练习时才有userId
    var userId = req.query.userId

    if (!util.isValidTestType(type)) {
      return res.json({
        code: 122,
        msg: '非法类型'
      })
    }

    Test.remove({
      type: type,
      id: id,
      userId: userId
    }, function(err) {
      if (err) {
        console.log('testController remove fail. err = ' + err)
        return res.json({
          code: 177,
          msg: err
        })
      } else {
        //  如果是删除习题数据，查找有没有对应的答案数据，如果有就删除，如果没有就不做处理
        if (type == 1) {
          Test.findById({
            id: id,
            type: 3
          }, function(err, test) {
            if (err) {
              console.log('testController remove fail. err = ' + err)
              return res.json({
                code: 177,
                msg: err
              })
            }
            if (!test) {
              return res.json({
                code: 100,
                msg: '删除成功'
              })
            }
            Test.remove({
              type: 3,
              id: id
            }, function(err) {
              if (err) {
                console.log('testController remove fail. err =' + err)
                return res.json({
                  code: 177,
                  msg: err
                })
              } else {
                return res.json({
                  code: 100,
                  msg: '删除成功,并删除了答案'
                })
              }
            })

          })
        } else {
          return res.json({
            code: 100,
            msg: '删除成功'
          })
        }

      }
    })
  }
}